﻿using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.ComponentModel;
using System.Collections.Generic;
public class MekongAirlines : global::Airplane
{
    
    public void TimKiem()
    {
        BrowserSession b = new BrowserSession();
        b.Get("https://booking.airmekong.com.vn/SearchInfo.aspx");
        InitDiaDiem();
        b.FormElements["ControlGroupSearchInfoView$AvailabilitySearchInputSearchInfoView$RadioButtonMarketStructure"] = LoaiTK;
        //b.FormElements["originStation1"] = DiemDi;
        b.FormElements["ControlGroupSearchInfoView$AvailabilitySearchInputSearchInfoView$TextBoxMarketOrigin1"] = DiemDi;
        //b.FormElements["destinationStation1"] = DiemDen;
        b.FormElements["ControlGroupSearchInfoView$AvailabilitySearchInputSearchInfoView$TextBoxMarketDestination1"] = DiemDen;
        b.FormElements["ControlGroupSearchInfoView$AvailabilitySearchInputSearchInfoView$DropDownListMarketDay1"] = NgayDi;
        b.FormElements["ControlGroupSearchInfoView$AvailabilitySearchInputSearchInfoView$DropDownListMarketMonth1"] = ThangDi;
        b.FormElements["ControlGroupSearchInfoView$AvailabilitySearchInputSearchInfoView$DropDownListMarketDay2"] = NgayVe;
        b.FormElements["ControlGroupSearchInfoView$AvailabilitySearchInputSearchInfoView$DropDownListMarketMonth2"] = ThangVe;
        b.FormElements["ControlGroupSearchInfoView$AvailabilitySearchInputSearchInfoView$DropDownListSearchBy"] = "columnView";
        b.FormElements.Add("__VIEWSTATE", "/wEPDwUBMGRk77h9CNekodLMZhdJu3riPx9ebN8=");
        string response = b.Post("https://booking.airmekong.com.vn/SearchInfo.aspx");
        Document.LoadHtml(response);

        HtmlAgilityPack.HtmlNode divnode = Document.DocumentNode.SelectSingleNode(@"/html[1]/body[1]/div[2]/div[3]/div[2]/div[2]/table[1]/tr[3]/td[1]");
        
        int i=3;
        while (divnode != null)
        {
            thongtincb info = new thongtincb();
            initCB(info, i);
            i++;
            string temp = string.Format("/html[1]/body[1]/div[2]/div[3]/div[2]/div[2]/table[1]/tr[{0}]/td[1]", i);
            divnode = Document.DocumentNode.SelectSingleNode(temp);
        }
        if (LoaiTK == "RoundTrip")
        {
            divnode = Document.DocumentNode.SelectSingleNode(@"/html[1]/body[1]/div[2]/div[3]/div[2]/div[4]/table[1]/tr[3]/td[1]");
            int j = 3;
            while (divnode != null)
            {
                thongtincb info = new thongtincb();
                initKhuHoi(info, j);
                j++;
                string temp = string.Format("/html[1]/body[1]/div[2]/div[3]/div[2]/div[4]/table[1]/tr[{0}]/td[1]", j);
                divnode = Document.DocumentNode.SelectSingleNode(temp);
            }
        }

    }

    private void initKhuHoi(thongtincb info, int i)
    {
        HtmlAgilityPack.HtmlNode divnode;
        string temp = string.Format("/html[1]/body[1]/div[2]/div[3]/div[2]/div[4]/table[1]/tr[{0}]/td[{1}]", i, 8);
        divnode = Document.DocumentNode.SelectSingleNode(temp);
        info.Giave = divnode.InnerText.Trim();
        if (info.Giave == "Hết vé")
        {
            temp = string.Format("/html[1]/body[1]/div[2]/div[3]/div[2]/div[4]/table[1]/tr[{0}]/td[{1}]", i, 7);
            divnode = Document.DocumentNode.SelectSingleNode(temp);
            info.Giave = divnode.InnerText.Trim();
        }
        if (info.Giave == "Hết vé")
        {
            temp = string.Format("/html[1]/body[1]/div[2]/div[3]/div[2]/div[4]/table[1]/tr[{0}]/td[{1}]", i, 6);
            divnode = Document.DocumentNode.SelectSingleNode(temp);
            info.Giave = divnode.InnerText.Trim();
        }
        if (info.Giave == "Hết vé")
        {
            temp = string.Format("/html[1]/body[1]/div[2]/div[3]/div[2]/div[4]/table[1]/tr[{0}]/td[{1}]", i, 5);
            divnode = Document.DocumentNode.SelectSingleNode(temp);
            info.Giave = divnode.InnerText.Trim();
        }
        if (info.Giave == "Hết vé")
        {
            temp = string.Format("/html[1]/body[1]/div[2]/div[3]/div[2]/div[4]/table[1]/tr[{0}]/td[{1}]", i, 4);
            divnode = Document.DocumentNode.SelectSingleNode(temp);
            info.Giave = divnode.InnerText.Trim();
        }
        if (info.Giave == "Hết vé")
            return;
        info.DiemDi = DiemDi;
        info.DiemDen = DiemDen;
        info.Hang = "Mekong AirLines";
        temp = string.Format("/html[1]/body[1]/div[2]/div[3]/div[2]/div[4]/table[1]/tr[{0}]/td[{1}]", i, 1);
        divnode = Document.DocumentNode.SelectSingleNode(temp);
        info.Chuyenbay = divnode.InnerText.Trim();

        temp = string.Format("/html[1]/body[1]/div[2]/div[3]/div[2]/div[4]/table[1]/tr[{0}]/td[{1}]", i, 2);
        divnode = Document.DocumentNode.SelectSingleNode(temp);
        info.ThoiGianDi = divnode.InnerText.Trim();

        temp = string.Format("/html[1]/body[1]/div[2]/div[3]/div[2]/div[4]/table[1]/tr[{0}]/td[{1}]", i, 3);
        divnode = Document.DocumentNode.SelectSingleNode(temp);
        info.ThoiGianDen = divnode.InnerText.Trim();
        lKhuHoi.Add(info);
    }

    private void initCB(thongtincb info, int i)
    {
        HtmlAgilityPack.HtmlNode divnode;
        string temp = string.Format("/html[1]/body[1]/div[2]/div[3]/div[2]/div[2]/table[1]/tr[{0}]/td[{1}]", i, 8);
        divnode = Document.DocumentNode.SelectSingleNode(temp);
        info.Giave = divnode.InnerText.Trim();
        if (info.Giave == "Hết vé")
        {
            temp = string.Format("/html[1]/body[1]/div[2]/div[3]/div[2]/div[2]/table[1]/tr[{0}]/td[{1}]", i, 7);
            divnode = Document.DocumentNode.SelectSingleNode(temp);
            info.Giave = divnode.InnerText.Trim();
        }
        if (info.Giave == "Hết vé")
        {
            temp = string.Format("/html[1]/body[1]/div[2]/div[3]/div[2]/div[2]/table[1]/tr[{0}]/td[{1}]", i, 6);
            divnode = Document.DocumentNode.SelectSingleNode(temp);
            info.Giave = divnode.InnerText.Trim();
        }
        if (info.Giave == "Hết vé")
        {
            temp = string.Format("/html[1]/body[1]/div[2]/div[3]/div[2]/div[2]/table[1]/tr[{0}]/td[{1}]", i, 5);
            divnode = Document.DocumentNode.SelectSingleNode(temp);
            info.Giave = divnode.InnerText.Trim();
        }
        if (info.Giave == "Hết vé")
        {
            temp = string.Format("/html[1]/body[1]/div[2]/div[3]/div[2]/div[2]/table[1]/tr[{0}]/td[{1}]", i, 4);
            divnode = Document.DocumentNode.SelectSingleNode(temp);
            info.Giave = divnode.InnerText.Trim();
        }
        if (info.Giave == "Hết vé")
            return;
        info.DiemDi = DiemDi;
        info.DiemDen = DiemDen;
        info.Hang = "Mekong AirLines";
        temp = string.Format("/html[1]/body[1]/div[2]/div[3]/div[2]/div[2]/table[1]/tr[{0}]/td[{1}]", i, 1);
        divnode = Document.DocumentNode.SelectSingleNode(temp);
        info.Chuyenbay = divnode.InnerText.Trim();
        
        temp = string.Format("/html[1]/body[1]/div[2]/div[3]/div[2]/div[2]/table[1]/tr[{0}]/td[{1}]", i, 2);
        divnode = Document.DocumentNode.SelectSingleNode(temp);
        info.ThoiGianDi = divnode.InnerText.Trim();

        temp = string.Format("/html[1]/body[1]/div[2]/div[3]/div[2]/div[2]/table[1]/tr[{0}]/td[{1}]", i, 3);
        divnode = Document.DocumentNode.SelectSingleNode(temp);
        info.ThoiGianDen = divnode.InnerText.Trim();
        lInfo.Add(info);
    }
}
